package com.yunwushanhe.student.controller;

import com.yunwushanhe.student.bean.ret.Result;
import com.yunwushanhe.student.bean.req.StudentAddReq;
import com.yunwushanhe.student.bean.req.PageReq;
import com.yunwushanhe.student.bean.req.StudentUpdateReq;
import com.yunwushanhe.student.entity.Student;
import com.yunwushanhe.student.service.StudentService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @PostMapping("/add")
    public Result<String> add(@RequestBody @Validated StudentAddReq addStudentInfo){
        studentService.add(addStudentInfo);
        return Result.ok("添加成功");
    }

    @PostMapping("/selectPage")
    public Result<List<Student>> selectPage(@RequestBody PageReq pageReq){
        return studentService.selectPage(pageReq);
    }

    @GetMapping("/deleteById")
    public Result<String> deleteById(Long id){
        if(id == null){
            return Result.fail("删除失败，控制器接收参数 id 不能为空！");
        }
        return studentService.deleteById(id);
    }

    /**
     * 通过学生id查询一个学生
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    public Result<Student> selectById(Long id){
        if(id == null){
            return Result.fail("查询失败，控制器接收参数 id 不能为空！");
        }
        return studentService.selectById(id);
    }

    @PostMapping("/updateById")
    public Result<String> updateById(@RequestBody @Validated StudentUpdateReq updateReq){
        return studentService.updateById(updateReq);
    }

    @GetMapping("/selectAll")
    public Result<List<Student>> selectAll(){ // 这个请求只能查前十条数据
        return studentService.selectAll();
    }
}
